﻿using LxCore;

namespace LxCore
{
    public class DefinitionsCenter : JsObject, IIdentityList
    {
        private UserParaCenter _UserPara = new UserParaCenter();
        public UserParaCenter UserPara
        {
            get { return _UserPara; }
            set { _UserPara = value; RaisePropertyChanged(nameof(UserPara)); }
        }
        private UserFieldCenter _UserField = new UserFieldCenter();
        public UserFieldCenter UserField
        {
            get { return _UserField; }
            set { _UserField = value; RaisePropertyChanged(nameof(UserField)); }
        }
        public SystemFieldCenter SystemField { get; private set; } = new SystemFieldCenter();
        public override void FromJsonKV(string key, IJsonReader jsReader)
        {
            switch (key)
            {
                case nameof(UserPara): jsReader.ReadObject(UserPara); break;
                case nameof(UserField): jsReader.ReadObject(UserField); break;
                default: base.FromJsonKV(key, jsReader); break;
            }
        }
        public override void ToJsonKV(IJsonWriter writer)
        {
            base.ToJsonKV(writer);
            writer.WriteObject(nameof(UserPara), UserPara);
            writer.WriteObject(nameof(UserField), UserField);
        }

        public int IndexOf(string name)
        {
            throw new NotImplementedException();
        }
        public bool HasSameName(string name, JsIdentifyItem self)
        {
            return hasSame(UserPara.Scalar) ||
               hasSame(UserPara.Vector) ||
               hasSame(UserField.Scalar) ||
               hasSame(UserField.Vector);//还要排除系统定义的
            bool hasSame<T>(IEnumerable<T> list) where T : IIdentity
            {
                foreach (var e in list)
                {
                    if (string.Equals(name, e?.Name) && !ReferenceEquals(e, self))
                    {
                        return true;
                    }
                }
                return false;
            }
        }

    }
}
